#include "main.h"

int BLOX2(const short gameType)
{
    //stop menu music
    oslStopSound(menu_music);
    
    //load some game music   
    OSL_SOUND * gameMusic = oslLoadSoundFileWAV((char*)"sounds/2.wav", OSL_FMT_NONE);
    oslPlaySound(gameMusic, 1); oslSetSoundLoop(gameMusic, 1);
    
    //reset game
    handle.ShowLoadScreen();
    handle.level = 1;
    handle.wave = 1;
    handle.background = handle.AdjustBackground();
    handle.NextWave();
    
    //make a player object
    PLAYER player;
    player.id = DEFAULT;
    player.image = player.LoadImage(DEFAULT);
    player.image->x = 240; player.image->y = 150;
    player.jumpHeight = 12;
    player.SpriteAnimate();
    
    while(!player.quit)
    {
        //update screen
        oslStartDrawing();
        oslClearScreen(BLACK);
        
        //update game
        handle.HANDLE();
        
        //update player
        player.Update();
        
        //update bullets
        for(int i = 0; i < 20; i++)bullet[i].exists = bullet[i].Update();
        
        if(player.toggle) {
		  oslIntraFontSetStyle(ltn, 0.5f,WHITE,BLACK,INTRAFONT_ALIGN_LEFT);
		  oslSetFont(ltn);
          oslDrawString(5, 5, "Health: %d", player.health);
          oslDrawString(5, 15, "Lives: %d", player.lives);
          oslDrawString(410, 5, "Level: %d", handle.level);
          oslDrawString(410, 15, "Wave: %d", handle.wave);
          oslDrawString(380, 25, "Enemies Left: %d",numOfEnemies);
        }
        
        oslEndDrawing();
        oslSyncFrame();
    }
    
    //free RAM
    oslStopSound(gameMusic); oslDeleteSound(gameMusic); 
    
    //reset menu music
    oslPlaySound(menu_music, 0); oslSetSoundLoop(menu_music, 1);
    
    return 0;
}

int main(int argc, char* argv[])
{
    //create an object of the CONTROLLER class
    CONTROLLER load;
    
    //Init graphics system
    load.oslib();
    
    //load fonts
    oslIntraFontInit(INTRAFONT_CACHE_ALL | INTRAFONT_STRING_UTF8);
    normal = oslLoadFontFile("flash0:/font/ltn0.pgf");
    bold = oslLoadFontFile("flash0:/font/ltn4.pgf");
    italic = oslLoadFontFile("flash0:/font/ltn2.pgf");
    oslIntraFontSetStyle(normal, 1.0f,WHITE,BLACK,INTRAFONT_ALIGN_LEFT);
    
    //Load menu sounds
    block = oslLoadSoundFileWAV((char*)"sounds/block.wav", OSL_FMT_NONE);
    jump = oslLoadSoundFileBGM((char*)"sounds/jump.bgm", OSL_FMT_NONE);
    rush = oslLoadSoundFileWAV((char*)"sounds/rush.wav", OSL_FMT_NONE);
    
    //Create a menu
    MainMenu.options = 7;
    menu_music = oslLoadSoundFileWAV((char*)"sounds/1.wav", OSL_FMT_NONE);
    select = oslLoadSoundFileWAV((char*)"sounds/select.wav", OSL_FMT_NONE);
    
    //Play menu music
    oslPlaySound(menu_music, 1); oslSetSoundLoop(menu_music, 1);
    
    //Show my logo
    load.VAUGHN_LOGO();

    static int status = 1;
    
    /***************** GAME LOOP **********************/
    while(1) {
      if(status == ACTIVE) status = MainMenu.ShowMainMenu();
      if(status == EXIT  ) break;      
    }
    
    oslDeleteFont(normal);
    oslDeleteFont(bold);
    oslDeleteFont(italic);
    oslIntraFontShutdown();
    oslEndGfx();
    oslQuit();
    
    return 0;
}

void PauseGame(bool &quit)
{
    while(1) {
        oslReadKeys();
        if(osl_keys->pressed.circle) break;
        else if(osl_keys->pressed.select){quit = true; break;}
        
        oslStartDrawing();
        oslPrintf_xy(200, 145, "PAUSED");
        oslEndDrawing();
        oslSyncFrame();
    }
    
    return;
}

int MISSING_FILES(int type)
{
    if(type == 1)      oslFatalError("At least one file is missing. Please check the img folder that came with this EBOOT!");
    else if(type == 2) oslFatalError("At least one file is missing. Please check the music folder that came with this EBOOT!");
    else if(type == 3) oslFatalError("YOU ARE MISSING TOO MANY FILES!!! RE-DOWNLOAD IMMEDIATELY!!!");
    else if(type == 4) oslFatalError("At least one file is missing. Please check the fonts folder that came with this EBOOT!");
    else if(type == 5) oslFatalError("ERROR: Blox 2 library corrupt either redownload or consult with V@ughn!!");
    
    return type;
}
        
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY )
{
    int collisio;
    collisio = 0;
    float img1width  = img1->stretchX;
    float img1height = img1->stretchY;
    float img2width  = img2->stretchX;
    float img2height = img2->stretchY;

    if ((img1posX + img1width > img2posX) &&
            (img1posX < img2posX + img2width) &&
            (img1posY + img1height > img2posY) &&
            (img1posY < img2posY + img2height) )
    {
        collisio = 1;
    }
    return collisio;
}
